package com.hunttown.mes.service;

import com.hunttown.common.domain.Query;
import com.hunttown.mes.dao.TransportOperateLogDao;
import com.hunttown.mes.domain.TransportOperateLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * created by wangjunfu on 2021-02-25 10:59:06
 */
@Service
public class TransportOperateLogService {

    private final static Logger logger = LoggerFactory.getLogger(TransportOperateLogService.class);

    private final TransportOperateLogDao transportOperateLogDao;
    private final AHelpService helpService;

    @Autowired
    public TransportOperateLogService(TransportOperateLogDao transportOperateLogDao, AHelpService helpService) {
        this.transportOperateLogDao = transportOperateLogDao;
        this.helpService = helpService;
    }

    /**
     * 新增
     *
     * @param transportOperateLog 实体对象
     * @return 最新插入id
     */
    public TransportOperateLog insert(TransportOperateLog transportOperateLog) {
        //api接口传进来是有值的
        if (transportOperateLog.getEntId() == null)
            transportOperateLog.setEntId(helpService.getEnterpriseId());

        return transportOperateLogDao.insert(transportOperateLog);
    }

    // 新增
    //modelType:1员工 2司机 3会员 4长期 5定时 6帐单 7退款
    public void insertSimple(Integer modelType, Integer tableid, Integer recordid, String title, String content, Integer opPeople, Integer entId) {
        TransportOperateLog dto = new TransportOperateLog();
        dto.setEntId(entId);
        dto.setModelType(modelType);
        dto.setTableId(tableid);
        dto.setRecordId(recordid);
        dto.setOpType(1);
        dto.setOpTitle(title);
        dto.setOpContent(content);
        dto.setOpPeople(opPeople);
        dto.setOpTime(new Date());
        dto.setOpIp("0.0.0.0");
        dto.setOpBrowser(opPeople > 0 ? "PC" : "worker");
        insert(dto);
    }

    /**
     * 根据ID修改数据信息
     *
     * @param transportOperateLog 实体对象
     * @return True or False
     */
    public Boolean updateInfoById(TransportOperateLog transportOperateLog) {
        if (transportOperateLog.getEntId() == null) {
            transportOperateLog.setEntId(helpService.getEnterpriseId());
        }
        return transportOperateLogDao.updateInfoById(transportOperateLog);
    }

    /**
     * 根据Query修改数据信息
     *
     * @param query 需要修改的字段 KV 格式
     * @return True or False
     */
    public Boolean updateInfoByQuery(Query query) {
        if (!query.containsKey("w_entId")) {
            query.put("w_entId", helpService.getEnterpriseId());
        }
        return transportOperateLogDao.updateInfoByQuery(query);
    }

    /**
     * 根据ID获取记录信息
     *
     * @param id 主键id
     * @return 实体对象
     */
    public TransportOperateLog selectById(Integer id) {
        return transportOperateLogDao.selectById(id, helpService.getEnterpriseId());
    }

    /**
     * 根据query取出一条记录，但是可能有多条记录，取最新的一条。
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象
     */
    public TransportOperateLog selectObjByQuery(Query query) {
        if (query == null || query.size() == 0) {
            return null;
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        List<TransportOperateLog> list = selectByQuery(query);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    //worker任务用，不涉及企业
    public TransportOperateLog selectObjWithoutEnt(Query query) {
        if (query == null || query.size() == 0) {
            return null;
        }

        List<TransportOperateLog> list = transportOperateLogDao.selectByQuery(query);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    /**
     * 根据条件获取List
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象列表
     */
    public List<TransportOperateLog> selectByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return transportOperateLogDao.selectByQuery(query);
    }

    /**
     * 根据条件获取记录总数
     *
     * @param query 查询条件 KV 格式
     * @return 总记录数
     */
    public Long countByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return transportOperateLogDao.countByQuery(query);
    }

    /**
     * 根据ID删除记录信息
     *
     * @param id 主键id
     * @return True or False
     */
    public Boolean deleteById(Integer id) {
        return transportOperateLogDao.deleteById(id, helpService.getEnterpriseId());
    }
}